<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Jobs\SyncCheckSmsBalance;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Facades\Cache;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        SyncCheckSmsBalance::dispatch();

        $msgApps = Cache::get('appMsgBalance',  [
            "phone" => [
                "name" => '验证短信',
                'time' => null,
                'status' => null,
                'balance' => null,
            ],
            "card" => [
                "name" => '营销短信',
                'time' => null,
                'status' => null,
                'balance' => null,
            ]
        ]);

        $msgAppBoxes = [];
        foreach($msgApps as $action => $msgApp) {
            $boxContent = $msgApp['balance'] ? "剩余条数：". $msgApp['balance'] ." - ": "剩余条数：未知 - ";
            $boxContent .= $msgApp['time'] ? "". date("m-d H:i:s",$msgApp['time']): "0-0 0:0:0";
            $msgAppBoxes[$action] = new Box($msgApp['name'], $boxContent);
        }

        return $content
            ->title('工作台')
            ->description('&nbsp;')
            ->row(function (Row $row) use ($msgAppBoxes){
                $row->column(4, function (Column $column) use ($msgAppBoxes) {
                    $box = new Box('Box标题', 'Box内容1');
                    $column->append($msgAppBoxes['phone']);
                });

                $row->column(4, function (Column $column) use ($msgAppBoxes) {
                    $column->append($msgAppBoxes['card']);
                });

                $row->column(4, function (Column $column) {
                    $box = new Box('Box标题', 'Box内容3');
                    $column->append($box);
                });
            } )
            ->row(function (Row $row) {

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::environment());
                });

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::extensions());
                });

                $row->column(4, function (Column $column) {
                    $column->append(Dashboard::dependencies());
                });
            });
    }

    public function demo(Content $content)
    {
        $headers = ['Id', 'Email', 'Name', 'Company'];
        $rows = [
            [1, 'labore21@yahoo.com', 'Ms. Clotilde Gibson', 'Goodwin-Watsica'],
            [2, 'omnis.in@hotmail.com', 'Allie Kuhic', 'Murphy, Koepp and Morar'],
            [3, 'quia65@hotmail.com', 'Prof. Drew Heller', 'Kihn LLC'],
            [4, 'xet@yahoo.com', 'William Koss', 'Becker-Raynor'],
            [5, 'ipsa.aut@gmail.com', 'Ms. Antonietta Kozey Jr.'],
        ];

        $table1 = new Table($headers, $rows);

        return $content
            ->title('Demo')
            ->description('&nbsp;')
            ->body($table1);

        //$content
        //    ->title('Demo')
        //    ->description('&nbsp;')
        //    ->view('demo', ['data' => 'foo']);
    }
}
